package servlets1;

import beans1.Doctorbean;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import service1.DoctorService;

import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;

@WebServlet("/Main_Doctor")
public class Main_Doctor extends HttpServlet {
    @SneakyThrows
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action=request.getParameter("action");
        System.out.println("in Main.java,the action is "+action);
        if("delDoctor".equals(action))
        {
            System.out.println("i am in delDoctor");
            delDoctor(request, response);
        }

        else if("editDoctor".equals(action)) {
            System.out.println("i am in editDoctor");
            try {
				editDoctor(request, response);
			} catch (ParseException | IOException | SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            System.out.println("i am out editDoctor");
        } else if ("insertDoctor".equals(action)) {
            System.out.println("i am in insertPatient");
            try {
                insertDoctor(request,response);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        } else
        {
            System.out.println("i am in showlist");
            showlist(request, response);
        }
    }

    private void insertDoctor(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException, ParseException {
        DoctorService doctorService=new DoctorService();
        String id = request.getParameter("id");
        String password = request.getParameter("password");
        String name = request.getParameter("name");
        String idNumber = request.getParameter("idnumber");
        String sex = request.getParameter("sex");
        String ksid = request.getParameter("ksid");
        String photo = request.getParameter("photo");
        String introduction = request.getParameter("introduction");
        String type = request.getParameter("type");
        Doctorbean str=new Doctorbean(id,password,name,idNumber,sex,ksid,photo,introduction,type);
        System.out.println("完成insertDoctor函数");
        doctorService.insertDoctor(str);
        response.sendRedirect(request.getContextPath()+"/Main_Doctor");
    }

    private void editDoctor(HttpServletRequest request, HttpServletResponse response) throws ParseException, IOException, SQLException {
        DoctorService doctorService=new DoctorService();
        String id = request.getParameter("id");
        String password = request.getParameter("password");
        String name = request.getParameter("name");
        String idNumber = request.getParameter("idnumber");
        String sex = request.getParameter("sex");
        String ksid = request.getParameter("ksid");
        String photo = request.getParameter("photo");
        String introduction = request.getParameter("introduction");
        String type = request.getParameter("type");
        Doctorbean str=new Doctorbean(id,password,name,idNumber,sex,ksid,photo,introduction,type);
        doctorService.editDoctor(str);
        response.sendRedirect(request.getContextPath()+"/Main_Doctor");
    }

    private void delDoctor(HttpServletRequest request, HttpServletResponse response) throws IOException {
        DoctorService doctorService=new DoctorService();
        Doctorbean str=new Doctorbean();
        str.setId(request.getParameter("id"));
        System.out.println("the id from str is "+str.getId()+" and the request is "+request.getParameter("id"));
        int result=doctorService.delDoctor(str);
        if(result==1) {
            System.out.println("success");
        }
        else {
            System.out.println("false");
        }
        response.sendRedirect(request.getContextPath()+"/Main_Doctor");
    }

    private void showlist(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        DoctorService doctorService=new DoctorService();
        ArrayList<Doctorbean> patientbeans= doctorService.browse();
        request.setAttribute("list",patientbeans);
        request.getRequestDispatcher("admin/doctureManage.jsp").forward(request,response);
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
